@*
* Copyright 2015 Yahoo Inc. Licensed under the Apache License, Version 2.0
* See accompanying LICENSE file.
*@
@import kafka.manager.utils.LongFormatted
@(cluster: String,
  topics: IndexedSeq[((String, Option[kafka.manager.model.ActorModel.TopicIdentity]),Boolean)],
  topicsUnderReassignment: IndexedSeq[String],
  pollConsumers: Boolean,
  displayTopicSize: Boolean
)(implicit messages: play.api.i18n.Messages, request:RequestHeader)

@getDeletedLevel(deleted: Boolean) = {
    @deleted match {
        case true => {table-danger}
        case i => {}
    }
}

@getBrokersSpreadLevel(percentage: Int) = {
    @percentage match {
        case i if i > 50 && i <= 75 => {table-warning}
        case i if i <= 50 => {table-danger}
        case i => {}
    }
}

@getBrokersSkewLevel(percentage: Int) = {
    @percentage match {
        case i if i > 0 && i <= 33 => {table-warning}
        case i if i >= 34 => {table-danger}
        case i => {}
    }
}

@getBrokersLeaderSkewLevel(percentage: Int) = {
    @percentage match {
        case i if i > 0 && i <= 33 => {table-warning}
        case i if i >= 34 => {table-danger}
        case i => {}
    }
}

@getUnderReplicatedLevel(percentage: Int) = {
    @percentage match {
        case i if i > 0 && i <= 33 => {table-warning}
        case i if i >= 34 => {table-danger}
        case i => {}
    }
}

@getReassignmentStatus(topic: String) = {
    @if(topicsUnderReassignment.contains(topic)) {glow-red} else {}
}

<table class="table" id="topics-table">
    <thead>
    <tr><th>Topic</th>
        <th># Partitions</th>
        <th># Brokers</th>
        <th><span title="Percentage of cluster brokers having partitions from the topic">Brokers Spread %</span></th>
        <th><span title="Percentage of brokers having more partitions than the average">Brokers Skew %</span></th>
        <th><span title="Percentage of brokers having more partitions as leader than the average">Brokers Leader Skew %</span></th>
        <th># Replicas</th>
        <th><span title="Percentage of partitions having a missing replica">Under Replicated %</span></th>
        @if(displayTopicSize){
        <th>Leader Size</th>
        }
        @if(pollConsumers){
        <th>Producer Message/Sec</th>
        <th>Summed Recent Offsets</th>
        }
    </tr>
    </thead>
    <tbody>
    @for( ((topic, topicIdentity), deleted) <- topics) {
      <tr class="@getReassignmentStatus(topic)">
          <td class="@getDeletedLevel(deleted)"><a href="@routes.Topic.topic(cluster,topic)">@topic</a></td>
          @topicIdentity.map{ ti => <td>@ti.partitions</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td>@ti.topicBrokers</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td class="@getBrokersSpreadLevel(ti.brokersSpreadPercentage)">@ti.brokersSpreadPercentage</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td class="@getBrokersSkewLevel(ti.brokersSkewPercentage)">@ti.brokersSkewPercentage</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td class="@getBrokersLeaderSkewLevel(ti.brokersLeaderSkewPercentage)">@ti.brokersLeaderSkewPercentage</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td>@ti.replicationFactor</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td class="@getUnderReplicatedLevel(ti.underReplicatedPercentage)">@ti.underReplicatedPercentage</td>}.getOrElse{<td> </td>}
          @if(displayTopicSize){
          @topicIdentity.map{ ti => <td>@ti.size</td>}.getOrElse{<td> </td>}
          }
          @if(pollConsumers){
          @topicIdentity.map{ ti => <td>@ti.producerRate</td>}.getOrElse{<td> </td>}
          @topicIdentity.map{ ti => <td>@ti.summedTopicOffsets.formattedAsDecimal</td>}.getOrElse{<td> </td>}
          }
      </tr>
    }
    </tbody>
</table>
